'use strict';
/**
 * @name 莫凡服务端 业务层
 * @version 0.0.1
 * @author ljr
 * @Time 2020年10月21日11:06:27
 */
const Service = require('egg').Service;

const operation_log = '[morefun:操作日志]';

class viewService extends Service {
  async insert(data) {
    const {
      app,
      ctx
    } = this;
    let { json, main_user, t_id,user_id,view_name } = data
    let params = {
      json, main_user, t_id,user_id,view_name,
      gmt_create: new Date(),
      gmt_modified: new Date(),
    };


    //添加用户记录template
    let result = await app.mysql.insert('view_data', params);
    if (result.insertId != 0) {
      return { isOk: 1, data: { id: result.insertId, ...params } };
    } else {
      return { isOk: 0, msg: '注册失败，请重试' }
    }
  }


  async findAll(data) {
    const {
      app,
      ctx
    } = this;
    let { main_user , curPage = 1, page_size = 10} = data

    let count = await app.mysql.query(`SELECT count(0) as count FROM view_data where main_user='${main_user}'`, '');
    let viewSql = `SELECT id, id as v_id,json, main_user,t_id,view_name FROM view_data where main_user='${main_user}'  limit ${(curPage - 1) * page_size},${page_size}  `
    let viewLIst = await app.mysql.query(viewSql, '');

    return { isOk: 1, data: { viewLIst,curPage, page_size, pageTotal: count[0].count  } };
  }

  async selectAll(data) {
    const {
      app,
      ctx
    } = this;
    let { main_user , t_id} = data

    let viewSql = `SELECT id, id as v_id,json, main_user,t_id,view_name FROM view_data where main_user='${main_user}' and  t_id=${t_id}`
    let viewLIst = await app.mysql.query(viewSql, '');

    return { isOk: 1, data: { viewLIst } };
  } 

  async findOne(data) {
    const {
      app,
      ctx
    } = this;
    let {id} = data

    let viewSql = `SELECT id,json FROM view_data where id='${id}'`
    let viewData = await app.mysql.query(viewSql, '');

    return { isOk: 1, data: { ...viewData} };
  }




  async update(data) {
    const {
      app,
      ctx
    } = this;
    let { id, json, main_user, t_id ,view_name} = data

    let isHaveSql = `SELECT count(0) as count FROM view_data where id='${id}'`
    let isHave = await app.mysql.query(isHaveSql, '');
    if (isHave[0].count) {

      let updateSql = `update view_data set json='${json}',t_id='${t_id}',view_name='${view_name}',gmt_modified=now() where id= '${id}'and main_user=${main_user}`
      let result = await app.mysql.query(updateSql, '');
      if (result.affectedRows != 0) {
        return { isOk: 1, data: result };
      } else {
        return { isOk: 0, msg: '更新失败，请重试' }
      }

    } else {
      return { isOk: 0, msg: '数据不存在' }
    }
  }

  async delete(data) {
    const {
      app,
      ctx
    } = this;
    let { id,main_user } = data
    let isHaveSql = `SELECT count(0) as count FROM view_data where id='${id}'`
    let isHave = await app.mysql.query(isHaveSql, '');
    if (isHave[0].count) {
      let deleteSql = `delete FROM view_data where id= '${id}' and main_user='${main_user}' `
      let result = await app.mysql.query(deleteSql, '');
      if (result.affectedRows != 0) {
        return { isOk: 1, msg: '删除成功' };
      } else {
        return { isOk: 0, msg: '删除失败，请重试' }
      }

    } else {
      return { isOk: 0, msg: '数据不存在' }
    }
  }


  async testPost(main_user) {
    const {
      app,
    } = this;
    const result = {};
    result.main_user = main_user;

    app.logger.info(`${operation_log}删除了id为${main_user}的用户`);

    return result;
  }
}

module.exports = viewService;